/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;

namespace Xceed.Silverlight.Data.Stats
{
  public class HarmonicMeanFunction : CumulativeStatFunction
  {
    public HarmonicMeanFunction()
      : base()
    {
    }

    public HarmonicMeanFunction( string resultPropertyName, string sourcePropertyName )
      : base( resultPropertyName, sourcePropertyName )
    {
    }

    protected internal override void Validate()
    {
      base.Validate();
      this.ValidateSourcePropertyName( 1 );
    }

    protected override void Initialize( Type[] sourcePropertyTypes )
    {
      base.Initialize( sourcePropertyTypes );

      switch( Type.GetTypeCode( sourcePropertyTypes[ 0 ] ) )
      {
        case TypeCode.Char:
        case TypeCode.Byte:
        case TypeCode.SByte:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
        case TypeCode.Single:
        case TypeCode.Double:
          m_accumulationType = TypeCode.Double;
          break;

        case TypeCode.Decimal:
          m_accumulationType = TypeCode.Decimal;
          break;
      }
    }

    protected internal override void Reset()
    {
      m_doubleSum = 0d;
      m_decimalSum = 0m;
      m_count = 0;
    }

    protected internal override void Accumulate( object[] values )
    {
      if( StatFunction.CanProcessValues( values, 1 ) )
      {
        checked
        {
          switch( m_accumulationType )
          {
            case TypeCode.Double:
              {
                double dblValue = Convert.ToDouble( values[ 0 ] );

                if( dblValue <= 0d )
                  throw new InvalidValueException( "The harmonic mean can only be calculated on elements whose values are greater than zero." );

                m_doubleSum += 1d / dblValue;
                m_count++;
              }
              break;

            case TypeCode.Decimal:
              {
                Decimal decValue = Convert.ToDecimal( values[ 0 ] );

                if( decValue <= 0m )
                  throw new InvalidValueException( "The harmonic mean can only be calculated on elements whose values are greater than zero." );

                m_decimalSum += 1 / decValue;
                m_count++;
              }
              break;
          }
        }
      }
    }

    protected internal override void AccumulateChildResult( StatResult childResult )
    {
      HarmonicMeanResult harMeanResult = ( HarmonicMeanResult )childResult;

      checked
      {
        switch( m_accumulationType )
        {
          case TypeCode.Double:
            {
              m_doubleSum += Convert.ToDouble( harMeanResult.Sum );
              m_count += harMeanResult.Count;
            }
            break;

          case TypeCode.Decimal:
            {
              m_decimalSum += Convert.ToDecimal( harMeanResult.Sum );
              m_count += harMeanResult.Count;
            }
            break;
        }
      }
    }

    protected internal override StatResult GetResult()
    {
      object value = null;
      object sum = null;

      if( m_count > 0 )
      {
        checked
        {
          switch( m_accumulationType )
          {
            case TypeCode.Double:
              {
                System.Diagnostics.Debug.Assert( m_doubleSum > 0d );
                value = Convert.ToDouble( m_count ) / m_doubleSum;
                sum = m_doubleSum;
              }
              break;

            case TypeCode.Decimal:
              {
                System.Diagnostics.Debug.Assert( m_decimalSum > 0m );
                value = Convert.ToDecimal( m_count ) / m_decimalSum;
                sum = m_decimalSum;
              }
              break;
          }
        }
      }

      return new HarmonicMeanResult( value, sum, m_count );
    }

    private TypeCode m_accumulationType = TypeCode.Empty;
    private double m_doubleSum = 0d;
    private decimal m_decimalSum = 0m;
    private long m_count = 0;

    private class HarmonicMeanResult : StatResult
    {
      public HarmonicMeanResult( object value, object sum, long count )
        : base( value )
      {
        m_sum = sum;
        m_count = count;
      }

      #region Sum Property

      public object Sum
      {
        get
        {
          return m_sum;
        }
      }

      private object m_sum;

      #endregion

      #region Count Property

      public long Count
      {
        get
        {
          return m_count;
        }
      }

      private long m_count;

      #endregion
    }
  }
}
